﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using NSoup.Nodes;
using System.Collections;
using Com.Toook.AutoDownloadHtml.Entity;
using Com.Toook.AutoDownloadHtml.Util;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml.NewsHtml
{
    /// <summary>
    /// 搜狐国内
    /// </summary>
    public class SohuNative : CommonSohuMethod
    {
        public SohuNative()
        {
            this.urlCategory = "http://news.sohu.com/guoneixinwen.shtml";
        }
        public SohuNative(string url)
            : base(url)
        {
        }
        public SohuNative(string url, string charset)
            : base(url, charset)
        {
        }
        /// <summary>
        /// 获取每个分类页面中的新闻url列表(如国内新闻中的每条新闻url)
        /// 保存结果到this.sdNewsUrls 字典StringDictionary
        /// </summary>       
        protected override void GetNewsUrls()
        {
            string hrefStart = "http://news.sohu.com/" + DateTime.Now.ToString("yyyyMMdd");
            this.sdNewsUrls = new StringDictionary();

            NSoup.Select.Elements divs = docCategory.GetElementsByClass("f14list");            
            foreach(NSoup.Nodes.Element div in divs)
            {
                NSoup.Select.Elements links = div.GetElementsByTag("a");
                base.AddLinksToStringDictionary(links, hrefStart);
                //foreach(NSoup.Nodes.Element link in links)
                //{
                //    if(link.Attr("href").StartsWith(hrefStart))
                //    {
                //        this.sdNewsUrls.Add(link.Attr("abs:href"), link.Text());
                //    }
                //}
            }
        }
        /// <summary>
        /// 获取分类中的每个新闻内容
        /// </summary>       
        protected override void GetEachNewsAndSave()
        {
            base.CommonGetEachNewsAndSave("NativeSohu");
            //Document docDetail;//新闻内容页面Document对象
            //string sourceUrl;//新闻来源url
            
            //foreach(DictionaryEntry d in this.sdNewsUrls)
            //{
            //    sourceUrl = d.Key.ToString();
            //    docDetail = GetDocument(sourceUrl);
            //    //删除新闻内容中的脚本
            //    docDetail.GetElementById("contentText").GetElementsByTag("script").Remove();

            //    this.news = new News();
            //    this.news.Title = docDetail.GetElementsByTag("h1").Html();
            //    this.news.Content = docDetail.GetElementById("contentText").Html();
            //    this.news.Id = SqliteHelper.GetNewsId();
            //    this.news.SourceUrl = sourceUrl;
            //    this.news.PubDate = SqliteHelper.GetNewsPubDateNow();
            //    this.news.TypeSite = "NativeSohu";

            //    this.SaveNewsContent();                
            //}
        }
    }
}
